<?php
if(!defined('FROM_INDEX'))exit('Invalid request');

/**
 * 
 * Make thumb image
 * 
 * @param string $src: Source image file
 * @param string $dst: Destination image file
 * @param int $width: Thumb image width
 * @param int $height: Thumb image height
 * @param bool $is_resize: If true, width/height = $width/$height, else keep source width/height
 * 
 * @return true | false
 */
function image_resize($src, $dst, $width, $height, $is_resize=false)
{
	$types = array(1 => 'gif', 2 => 'jpg', 3 => 'png', 6 => 'bmp');
	
	if(!list($w, $h, $type) = getimagesize($src)) return false; //Unsupported picture type!

	$type = isset($types[$type]) ? $types[$type] : '';
	
	switch($type)
	{
		case 'bmp': $img = @imagecreatefromwbmp($src); break;
		case 'gif': $img = @imagecreatefromgif($src); break;
		case 'jpg': $img = @imagecreatefromjpeg($src); break;
		case 'png': $img = @imagecreatefrompng($src); break;
		default : return false;//Unsupported picture type!
	}
	
	if(!$img) return false;

	$x = $y = 0;
	
	// resize
	if($is_resize)
	{
		//if($w < $width || $h < $height) return false;//Picture is too small!
		if($w < $width && $h < $height) return false;//Picture is too small!
		$ratio = max($width/$w, $height/$h);
		$x = round(($w - $width / $ratio) / 2, 2);
		$y = round(($h - $height / $ratio) / 2, 2);
		$w = $width / $ratio;
		$h = $height / $ratio;
	}
	else
	{
		if($w < $width && $h < $height) return false;//Picture is too small!
		$ratio = min($width/$w, $height/$h);
		$width = $w * $ratio;
		$height = $h * $ratio;
	}
	
	$new = @imagecreatetruecolor($width, $height);

	// preserve transparency
	if($type == 'gif' || $type == 'png')
	{
		@imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
		@imagealphablending($new, false);
		@imagesavealpha($new, true);
	}
	
	@imagecopyresampled($new, $img, 0, 0, $x, $y, $width, $height, $w, $h);

	switch($type)
	{
		case 'bmp': @imagewbmp($new, $dst); break;
		case 'gif': @imagegif($new, $dst); break;
		case 'jpg': @imagejpeg($new, $dst, 100); break;
		case 'png': @imagepng($new, $dst); break;
	}
	
	imagedestroy($new);
	imagedestroy($img);

	return true;
}

function is_image($filename, $allow_types=array('gif'=>1,'jpg'=>1,'png'=>1,'bmp'=>1,'jpeg'=>1))
{
	if(!is_file($filename))
	{
		return false;
	}

	$imagetypes = array('1'=>'gif','2'=>'jpg','3'=>'png','4'=>'swf','5'=>'psd','6'=>'bmp','7'=>'tiff','8'=>'tiff','9'=>'jpc','10'=>'jp2','11'=>'jpx','12'=>'jb2','13'=>'swc','14'=>'iff','15'=>'wbmp','16'=>'xbm',);
	if(!$allow_types)
	{
		$allow_types = array('gif'=>1,'jpg'=>1,'png'=>1,'bmp'=>1,'jpeg'=>1);
	}
	$typeid = 0;
	$imagetype = '';

	if(function_exists('exif_imagetype'))
	{
		$typeid = exif_imagetype($filename);
	}
	else if(function_exists('getimagesize'))
	{
		$_tmps = getimagesize($filename);
		$typeid = (int) $_tmps[2];
	}
	else
	{
		if(($fh = @fopen($filename, "rb")))
		{
			$strInfo = unpack("C2chars", fread($fh,2));
			fclose($fh);
			$fileTypes = array(7790=>'exe',7784=>'midi',8297=>'rar',255216=>'jpg',7173=>'gif',6677=>'bmp',13780=>'png',);
			$imagetype = $fileTypes[intval($strInfo['chars1'] . $strInfo['chars2'])];
		}
	}

	$file_ext = strtolower(trim(substr(strrchr($filename, '.'), 1)));

	if($typeid > 0)
	{
		$imagetype = $imagetypes[$typeid];
	}
	else
	{
		if(!$imagetype)
		{
			$imagetype = $file_ext;
		}
	}

	if($allow_types && $file_ext && $imagetype && isset($allow_types[$file_ext]) && isset($allow_types[$imagetype]))
	{
		return true;
	}

	return false;
}